/**
 * 
 */

$(function() {
	$(".error").hide();
	$(".new-button").click(function() {  
		$('.error').hide();  
		var term = encodeURIComponent($("input#term").val());
		if (term == "") {  
			$("label#term_error").show();  
			$("input#term").focus();  
			return false;  
		}
		var name = encodeURIComponent($("input#name").val());  
		if (name == "") {
			$("label#name_error").show();  
			$("input#name").focus();  
			return false;  
		}
		var type = encodeURIComponent($("input#type").val());  
		if (type == "") {  
			$("label#type_error").show();  
			$("input#type").focus();  
			return false;  
		}
		var bloombergId = encodeURIComponent($("input#bloombergId").val());
		var definition = encodeURIComponent($("input#definition").val());
		var currency = encodeURIComponent($("input#currency").val());  
		if (currency == "") {  
			$("label#currency_error").show();  
			$("input#currency").focus();  
			return false;  
		}
		var daycountConvention = encodeURIComponent($("input#daycountConvention").val());  
		if (daycountConvention == "") {  
			$("label#daycountConvention_error").show();  
			$("input#daycountConvention").focus();  
			return false;  
		}
		var compoundingFrequency = encodeURIComponent($("input#compoundingFrequency").val());  
		var dataString = 'term='+ term + '&name=' + name + '&type=' + type + '&bloombergId=' + bloombergId + '&definition=' + definition + 
						 '&currency=' + currency + '&daycountConvention=' + daycountConvention + '&compoundingFrequency=' + compoundingFrequency;  
		$.ajax({  
			type: "POST",  
			url: "StoreInstrument",  
			data: dataString,  
			success: function(msg) {  
				if(msg.indexOf("Error") == -1) {
					var jsonExpression = "(" + msg + ")";
					var instrument = eval(jsonExpression);
					addInstrument(instrument);
				} else {
					alert(msg);
				}
			}
		});
		return false;
	});  

	function addInstrument(instrument) {
		var rowPos = findPositionForNewInstrument(instrument);
		
		var tbl = document.getElementById('tblInstruments');
		var instrumentCount = tbl.rows.length-1;
		var row = tbl.insertRow(rowPos);
		row.setAttribute("id","row" + instrument.instrumentId);
		
		var cell = row.insertCell(0);
		var textNode = document.createTextNode(instrument.instrumentId);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.term);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.strTerm);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.name);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.type);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.bloombergId);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.definition);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.currency);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.daycountConvention);
		cell.appendChild(textNode);

		cell = row.insertCell(row.cells.length);
		textNode = document.createTextNode(instrument.compoundingFrequency);
		cell.appendChild(textNode);
		
		cell = row.insertCell(row.cells.length);
		var element = document.createElement("Button");
		element.appendChild(document.createTextNode("Delete"))
		element.className="button delete-button";
		element.setAttribute("onclick","deleteInstrument(" + instrument.instrumentId + ")");
		element.setAttribute("id","btn" + instrument.instrumentId);
		cell.appendChild(element);
	}

	function findPositionForNewInstrument(instrument) {
		var tbl = document.getElementById('tblInstruments');
		var count = tbl.rows.length;
		var i=1;
		if (count != 1) {
			while(i < count) {
				if(instrument.term < tbl.rows[i].cells[1].firstChild.nodeValue) {
					return i;
				}
				i++;
			}
		}
		return count;
	}
});

function deleteInstrument(instrumentId) {
	var dataString = 'instrumentId='+ encodeURIComponent(instrumentId);
	$.ajax({  
		type: "POST",  
		url: "DeleteInstrument",  
		data: dataString,  
		success: function(msg) {  
			if(msg.indexOf("Error") == -1) {
				document.getElementById('tblInstruments').deleteRow(document.getElementById("row" + instrumentId).rowIndex);
			} else {
				alert("error!");
			}
		}  
	});
}